using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class RangeReviewBenchmarksRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class RangeReviewBenchmarksDbo : DatabaseObject
        {
            public RangeReviewBenchmarksDbo()
            {
                Name = "RangeReviewBenchmarks";
                GeneratedClassName = "RangeReviewBenchmarks";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "RangeReviewSourceId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "RangeReviewSourceId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "RangeReviewTargetId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "RangeReviewTargetId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "StoreId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreGroupId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "StoreGroupId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TargetCurrencyId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "TargetCurrencyId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ColumnNumber", ColumnType = "tinyint", IsIdentity = false, IsNullable = false, Length = 1, Precision = 3, Scale = 0, GeneratedPropertyName = "ColumnNumber" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "HistoryGroupId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "HistoryGroupId" });
              
            }
        }
		
		public static List<RangeReviewBenchmarkRow> Select(Expression<Func<RangeReviewBenchmarkRow, bool>> expression)
        {
            List<RangeReviewBenchmarkRow> rows = new List<RangeReviewBenchmarkRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<RangeReviewBenchmarkRow>(new RangeReviewBenchmarksDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<RangeReviewBenchmarkRow> Select()
		{
			List<RangeReviewBenchmarkRow> rows = new List<RangeReviewBenchmarkRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<RangeReviewBenchmarkRow>(new RangeReviewBenchmarksDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<RangeReviewBenchmarkRow> PagedSelect(Expression<Func<RangeReviewBenchmarkRow, bool>> filterExpression, Expression<Func<RangeReviewBenchmarkRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<RangeReviewBenchmarkRow> rows = new List<RangeReviewBenchmarkRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<RangeReviewBenchmarkRow>(new RangeReviewBenchmarksDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<RangeReviewBenchmarkRow> RangeReviewBenchmarkList)
        {
            RangeReviewBenchmarkRow row;

            while (reader.Read())
            {
                row = new RangeReviewBenchmarkRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					RangeReviewSourceId = Util.CastDbValue<int>(reader["RangeReviewSourceId"]),
					RangeReviewTargetId = Util.CastDbValue<int?>(reader["RangeReviewTargetId"]),
					StoreId = Util.CastDbValue<int?>(reader["StoreId"]),
					StoreGroupId = Util.CastDbValue<int?>(reader["StoreGroupId"]),
					TargetCurrencyId = Util.CastDbValue<int>(reader["TargetCurrencyId"]),
					ColumnNumber = Util.CastDbValue<int>(reader["ColumnNumber"]),
					HistoryGroupId = Util.CastDbValue<int?>(reader["HistoryGroupId"])
                };
                                
                RangeReviewBenchmarkList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(RangeReviewBenchmarkRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<RangeReviewBenchmarkRow>(new RangeReviewBenchmarksDbo(), row);
            }
        }

		public static void Update(RangeReviewBenchmarkRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<RangeReviewBenchmarkRow>(new RangeReviewBenchmarksDbo(), row);
            }
        }

		public static void Delete(Expression<Func<RangeReviewBenchmarkRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<RangeReviewBenchmarkRow>(new RangeReviewBenchmarksDbo(), expression);
            }
        }

	}
}
	
